package com.hdjk.view.activity.personalcenter;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ListView;

import com.hdjk.view.R;
import com.hdjk.view.activity.financial.FundDetailActivity;
import com.hdjk.view.adapter.FavoriteListAdapter;
import com.hdjk.view.callback.CallBackListener;
import com.hdjk.view.entity.FundEntity;
import com.hdjk.view.network.FavoriteNetWork;
import com.hdjk.view.network.FundNetWork;
import com.hdjk.view.util.EmptyViewUtils;
import com.hdjk.view.util.UserTokenUtil;
import com.lst.projectlib.base.BaseActivity;
import com.lst.projectlib.entity.Msg;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 收藏夹页
 * Created by sxy on 2017/11/3.
 */

public class FavoriteActivity extends BaseActivity implements View.OnClickListener {
    private List<FundEntity> mList;
    private FavoriteListAdapter mAdapter;
    private int mPageIndex = 0;

    // Content View Elements
    private com.lst.projectlib.component.CustomView.TopTitleBar ttb_title_bar;
    //    private cn.finalteam.loadingviewfinal.PtrClassicFrameLayout ptr_layout;
    private ListView lv_data_list;
    private FrameLayout mFlEmptyView;
    // End Of Content View Elements

    private void bindViews() {
        ttb_title_bar = (com.lst.projectlib.component.CustomView.TopTitleBar) findViewById(R.id.ttb_title_bar);
//        ptr_layout = (cn.finalteam.loadingviewfinal.PtrClassicFrameLayout) findViewById(R.id.ptr_layout);
        mFlEmptyView = (FrameLayout) findViewById(R.id.fl_empty_view);
        lv_data_list = (ListView) findViewById(R.id.lv_data_list);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_favorite);
        bindViews();
        ttb_title_bar.findViewById(R.id.tvNavBack).setOnClickListener(this);
//        mFlEmptyView.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        mList = new ArrayList<>();
        mAdapter = new FavoriteListAdapter(this);
        mAdapter.setDataList(mList);

//        lv_data_list.setLoadMoreView(new DefaultLoadMoreView(this));
        lv_data_list.setAdapter(mAdapter);
        lv_data_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int index = (int) id;
                Intent intent = new Intent(FavoriteActivity.this, FundDetailActivity.class);
                intent.putExtra("id", mList.get(index).getFundCode());
                intent.putExtra("type", FundDetailActivity.TYPE_FUND);
                startActivity(intent);
            }
        });
//        lv_data_list.setEmptyView(mFlEmptyView);

//        ptr_layout.setOnRefreshListener(new OnDefaultRefreshListener() {
//            @Override
//            public void onRefreshBegin(PtrFrameLayout frame) {
//                // 下拉刷新
//                getNetworkList(true);
//            }
//        });
//        ptr_layout.setLastUpdateTimeRelateObject(this);
//        lv_data_list.setOnLoadMoreListener(new OnLoadMoreListener() {
//            @Override
//            public void loadMore() {
//                // 上拉加载
//                getNetworkList(false);
//            }
//        });
        getNetworkList(true);
    }

//    private void onLoaded() {
//        ptr_layout.onRefreshComplete();
//        lv_data_list.onLoadMoreComplete();
//    }

    private void getNetworkList(final boolean isDropDown) {
        mDialog.show();
        FavoriteNetWork.getFavoriteList(UserTokenUtil.getToken(this), new CallBackListener() {
            @Override
            public void onCallBackCompleted(Msg msg) {
//                onLoaded();
                if (msg != null && msg.getSuccess() && msg.getData() != null && msg.getData() instanceof String) {
                    String codelist = (String) msg.getData();
                    if (TextUtils.isEmpty(codelist)) {
                        mDialog.cancel();
                        return;
                    }
                    JSONObject object = new JSONObject();
                    try {
                        object.put("fundCodes", codelist);
                        FundNetWork.getFundList(UserTokenUtil.getToken(FavoriteActivity.this), object.toString(), new CallBackListener() {
                            @Override
                            public void onCallBackCompleted(Msg msg) {
                                mDialog.dismiss();
                                if (msg != null && msg.getSuccess() && msg.getData() != null && !TextUtils.isEmpty(msg.getData().toString())) {
                                    mList = (ArrayList) msg.getData();
                                    if (mAdapter == null) {
                                        mAdapter = new FavoriteListAdapter(FavoriteActivity.this);
                                        mAdapter.setDataList(mList);
                                        lv_data_list.setAdapter(mAdapter);
                                    } else {
                                        mAdapter.setDataList(mList);
                                        mAdapter.notifyDataSetChanged();
                                    }
                                }
                                if (mList == null || mList.size() <= 0) {
                                    EmptyViewUtils.showNoDataEmpty(mFlEmptyView);
                                    return;
                                }
                            }

                            @Override
                            public void onCallBackError(Msg msg) {
                                mDialog.cancel();
                            }
                        });
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                } else {
                    mDialog.cancel();
                }
            }

            @Override
            public void onCallBackError(Msg msg) {
                mDialog.cancel();
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvNavBack:
                back();
                break;
            case R.id.fl_empty_view:
                getNetworkList(true);
                break;
        }
    }
}
